package com.cashmama.market.api.domain.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.cashmama.market.api.domain.vo.InnerPushResponse;
import com.cashmama.market.api.domain.vo.openapi.OpenApiResponse;
import com.cashmama.market.api.domain.vo.openapi.request.PlatformSendData;
import com.cashmama.market.api.domain.vo.openapi.request.RepayRequest;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.enums.BasicErrorCodeEnum;
import com.fqgj.common.utils.HttpUtil;
import com.fqgj.common.utils.MD5;
import com.google.gson.Gson;
import com.jsoniter.output.JsonStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2020-12-02 11:49
 **/
@Component
public class CallHttpService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CallHttpService.class);

    @Autowired
    private ApolloConfigUtil apolloConfigUtil;

    /**
     * 调用商户http方法
     *
     * @param url
     * @param orgPublicKey
     * @param bizData
     */
//    public static OpenApiResponse callMerchantMethod(String url, String orgPublicKey, String method, String merchantCode, String bizData) {
//
//        OpenApiResponse openApiResponse = null;
//        try {
//            PlatformSendData sendData = new PlatformSendData(method, merchantCode, bizData).createSign(orgPublicKey);
//            LOGGER.info("====== 请求入参 url:{}, orgPublicKey:{}, param:{} =====", url, orgPublicKey, JSONObject.toJSONString(sendData));
//            Map<String, String> headers = new HashMap<>();
//            headers.put("Content-Type", "application/json");
//            String result = HttpUtil.postPageBody(url, headers, JSONObject.toJSONString(sendData));
//            LOGGER.info("==== 请求结果 result:{} =====", result);
//            openApiResponse = JSONObject.parseObject(result, OpenApiResponse.class);
//        } catch (Exception e) {
//            LOGGER.error("=== 请求异常 bizData:{}, e====", bizData, e);
//        }
//        return openApiResponse;
//    }
    public static void main(String[] args) {
        RepayRequest repayRequest = new RepayRequest("232452199593019960DC", "", "");

        Map<String, String> dd = new HashMap<>();
        dd.put("userName", "aa");
        dd.put("productCode", "rupeego_3000_7d");
        dd.put("mobileMd5", "a2e9e99616a21fb033d1e8bdf3ca128e");
        dd.put("aadhaarMd5", MD5.md5("726743114515"));

        String bizData = JSONObject.toJSONString(dd);
        Object rupeeGo = callMerchantMethod("https://dc-api.rupee-mama.in", "O70e6sdf10bee06", "/checkUser", "RupeeGo", bizData);
        System.err.println(11);

    }

    public static OpenApiResponse callMerchantMethod(String url, String orgPublicKey, String method, String merchantCode, String bizData) {

        try {
            LOGGER.info("== 请求 ==，bizData:{}", bizData);
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json;charset=utf-8");

            SendToMerchantData sendToMerchantData = new SendToMerchantData(merchantCode, bizData);
            sendToMerchantData.createSign(orgPublicKey);
            LOGGER.info("== 请求 ==，url:{}", url + "/" + method);
            HttpEntity requestPush = new HttpEntity(JsonStream.serialize(sendToMerchantData), headers);
            ResponseEntity responseEntity = new RestTemplate().postForEntity(url + "/" + method, requestPush, String.class);
            String responseStr = responseEntity.getBody().toString();
            LOGGER.info("=======  推送请求结果  ======:{}", responseStr);
            return new Gson().fromJson(responseStr, OpenApiResponse.class);
//            if (innerPushResponse.getCode() == BasicErrorCodeEnum.OPEN_API_SUCCESS.getCode()) {
//                LOGGER.info("== 获取成功 ==");
//                return innerPushResponse;
//            }
        } catch (Exception e) {
            LOGGER.error("== commonPost  failed ==,bizData:{}", bizData, e);
        }

        return null;
    }
}
